<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="页面名称" prop="pageName">
        <el-input
          v-model="queryParams.pageName"
          placeholder="请输入页面名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['page:page:add']"
        >新增页面</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="pageList">
      <el-table-column label="编号" align="center" prop="id" width="100" />
      <el-table-column label="页面名称" align="center" prop="pageName" />
      <el-table-column label="主页显示" align="center" prop="isHome">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.isHome"
            active-value="0"
            inactive-value="1"
            @change="handleIsHomeChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="页面备注" align="center" prop="remarks" width="500" />
      <el-table-column label="背景颜色" align="center" prop="backgroundColor" width="100" >
        <template slot-scope="scope">
          <div :style="'height: 24px;width: 100%;background-color:'+scope.row.backgroundColor "></div>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="state" width="150">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.state"
            active-value="0"
            inactive-value="1"
            @change="handleStateChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="250">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-set-up"
            @click="handlePageEdit(scope.row)"
            v-hasPermi="['page:page:edit']"
          >页面设计</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['page:page:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['page:page:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改页面管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="页面名称" prop="pageName">
          <el-input v-model="form.pageName" placeholder="请输入页面名称" />
        </el-form-item>
        <el-form-item label="页面颜色" prop="backgroundColor">
          <el-color-picker
            v-model="form.backgroundColor"
            :predefine="['#409EFF', '#1890ff', '#304156','#212121','#11a983', '#13c2c2', '#6959CD', '#f5222d', ]"
            class="myPicker"
            style="width: 100%;"
            popper-class="theme-picker-dropdown"
          />
        </el-form-item>
        <el-form-item label="页面备注" prop="remarks">
          <el-input type="textarea"  v-model="form.remarks" placeholder="请输入页面备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listPage, getPage, delPage, addPage, updatePage, changePageState, changeIsHome} from "@/api/page/page";

export default {
  name: "Page",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 页面管理表格数据
      pageList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 页面状态，0开启，1停用字典
      stateOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        pageName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        pageName: [
          { required: true, message: "页面名称不能为空", trigger: "blur" },
          { max: 20, message: '页面名称长度不能超过20个字符', trigger: 'blur' }
        ],
        backgroundColor: [
          { required: true, message: "页面背景颜色，默认为白色不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("sys_normal_disable").then(response => {
      this.stateOptions = response.data;
    });
  },
  methods: {
    /** 查询页面管理列表 */
    getList() {
      this.loading = true;
      listPage(this.queryParams).then(response => {
        this.pageList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        pageName: null,
        isHome: '1',
        remarks: null,
        backgroundColor: '#ffffff',
        state: 1
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加页面管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id;
      getPage(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改页面管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updatePage(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPage(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id;
      this.$confirm('是否确认删除页面管理编号为"' + id + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delPage(id);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    // 状态修改
    handleStateChange(row) {
      let text = row.state === "0" ? "启用" : "停用";
      this.$confirm('确认要"' + text + '""' + row.pageName + '"页面吗?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return changePageState(row.id, row.state);
      }).then(() => {
        this.msgSuccess(text + "成功");
      }).catch(function() {
        row.state = row.state === "0" ? "1" : "0";
      });
    },
    handlePageEdit(row){
      this.$router.push({
        name:"pageEdit",
        query: row
      })
    },
    // 状态修改
    handleIsHomeChange(row) {
      let text = row.isHome === "0" ? "设置" : "取消";
      this.$confirm('确认要"' + text + '""' + row.pageName + '"主页面吗?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return changeIsHome(row.id, row.isHome);
      }).then(() => {
        this.msgSuccess(text + "成功");
        this.getList();
      }).catch(function() {
        row.isHome = row.isHome === "0" ? "1" : "0";
      });
    },
  }
};
</script>
<style lang="scss">
.myPicker .el-color-picker__trigger{
  width: 100%!important;
}
</style>
